package com.smartens.boit.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.smartens.boit.R;
import com.smartens.boit.bean.ControlBean;
import com.smartens.boit.bean.LightDetailBean;
import com.smartens.boit.bean.ListParameterBean;
import com.smartens.boit.util.HttpHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 自动控制适配器
 * @Author songtt
 * @Date 2021/3/4
 */

public class AutoAdapter extends RecyclerView.Adapter<AutoAdapter.ViewHolder> {
    private static final String TAG = "AutoAdapter";
    private List<List<LightDetailBean>> mList = new ArrayList<>();
    private Context context;
    private List<String> options1Items;
    private OnControlClickListener onControlClickListener;
    private OnBright02ClickListener OnBrightClickListener02;
    private OnBright01ClickListener OnBright01ClickListener;
    private OnBright03ClickListener OnBright03ClickListener;
    private OnLlAdjustBrightnessPlanClickListener onLlAdjustBrightnessPlanClickListener;
    private HttpHelper helper;

    public AutoAdapter(List<List<LightDetailBean>> list, Context context) {
        mList = list;
        this.context = context;
    }

    public void setData(List<List<LightDetailBean>> data) {
        this.mList = data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //将我们自定义的item布局R.layout.item_one转换为View
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_auto, parent, false);
        //将view传递给我们自定义的ViewHolder
        ViewHolder holder = new ViewHolder(view);
        helper = HttpHelper.getInstance();
        //返回这个MyHolder实体
        return holder;
    }

    /**
     * 通过方法提供的ViewHolder，将数据绑定到ViewHolder中
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (mList.size() > 0) {
            if (position == 0) {
                holder.name.setText("分组一设备设置");
            } else if (position == 1) {
                holder.name.setText("分组二设备设置");
            }

            if (mList.get(position) != null && mList.get(position).get(0) != null && mList.get(position).get(0).getProjectParameterList() != null) {
                Log.e(TAG, mList.get(position).get(0).getProjectParameterList().get(0).getTime() + " 亮度 " + mList.get(position).get(0).getProjectParameterList().get(0).getBrightness() + "%");
                Log.e(TAG, mList.get(position).get(1).getProjectParameterList().get(0).getTime() + " 亮度 " + mList.get(position).get(1).getProjectParameterList().get(0).getBrightness() + "%");
                Log.e(TAG, mList.get(position).get(2).getProjectParameterList().get(0).getTime() + " 亮度 " + mList.get(position).get(2).getProjectParameterList().get(0).getBrightness() + "%");
                if (mList.get(position).get(0).getProjectParameterList().get(0).getTime().equals("25:00")) {
                    holder.light01.setText("设置时段亮度");
                } else {
                    holder.light01.setText("时间 " + mList.get(position).get(0).getProjectParameterList().get(0).getTime() + " 亮度 " + mList.get(position).get(0).getProjectParameterList().get(0).getBrightness() + "%");
                }
                if (mList.get(position).get(1).getProjectParameterList().get(0).getTime().equals("25:00")) {
                    holder.light02.setText("设置时段亮度");
                } else {
                    holder.light02.setText("时间 " + mList.get(position).get(1).getProjectParameterList().get(0).getTime() + " 亮度 " + mList.get(position).get(1).getProjectParameterList().get(0).getBrightness() + "%");
                }
                if (mList.get(position).get(2).getProjectParameterList().get(0).getTime().equals("25:00")) {
                    holder.light03.setText("设置时段亮度");
                } else {
                    holder.light03.setText("时间 " + mList.get(position).get(2).getProjectParameterList().get(0).getTime() + " 亮度 " + mList.get(position).get(2).getProjectParameterList().get(0).getBrightness() + "%");
                }


            }
            holder.checkBox.setChecked("1".equals(mList.get(position).get(0).getDimmingMode()));

            holder.linShow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onControlClickListener != null) {
                        onControlClickListener.onItemClick(v, holder.getAdapterPosition());
                    }
                }
            });
            holder.swUp.setEnabled(false);
            holder.bright01.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (OnBright01ClickListener != null) {
                        OnBright01ClickListener.onItemClick(v, holder.getAdapterPosition());
                    }
                }
            });
            holder.bright02.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (OnBrightClickListener02 != null) {
                        OnBrightClickListener02.onItemClick(v, holder.getAdapterPosition());
                    }
                }
            });
            holder.bright03.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (OnBright03ClickListener != null) {
                        OnBright03ClickListener.onItemClick(v, holder.getAdapterPosition());
                    }
                }
            });
            holder.save.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    upDateManager(position,
                            mList.get(position).get(0).getProjectId(),
                            mList.get(position).get(0).getProjectname(),
                            mList.get(position).get(0).getDimmingMode(), getList(position));
                }
            });

            holder.llAdjustBrightnessPlan.setOnClickListener(v -> {
                if (onLlAdjustBrightnessPlanClickListener != null) {
                    onLlAdjustBrightnessPlanClickListener.onClickListener(position);
                }
            });
        }
    }

    /**
     * 获取数据源总的条数
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return mList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout gradual;
        CheckBox swUp;
        LinearLayout linShow;
        LinearLayout bright01;
        LinearLayout bright02;
        LinearLayout bright03;
        TextView light01;
        TextView light02;
        TextView light03;
        TextView name;
        CheckBox checkBox;
        TextView save;
        LinearLayout llAdjustBrightnessPlan;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            linShow = itemView.findViewById(R.id.lin_show);
            gradual = itemView.findViewById(R.id.lin_gradual);
            swUp = itemView.findViewById(R.id.sw_up);
            bright01 = itemView.findViewById(R.id.lin_01);
            bright02 = itemView.findViewById(R.id.lin_02);
            bright03 = itemView.findViewById(R.id.lin_03);
            light01 = itemView.findViewById(R.id.tv_bright1);
            light02 = itemView.findViewById(R.id.tv_bright2);
            light03 = itemView.findViewById(R.id.tv_bright3);
            name = itemView.findViewById(R.id.tv_name);
            checkBox = itemView.findViewById(R.id.sw_check);
            save = itemView.findViewById(R.id.tv_save);
            llAdjustBrightnessPlan = itemView.findViewById(R.id.llAdjustBrightnessPlan);
        }
    }

    public void setOnControlClickListener(OnControlClickListener onControlClickListener) {
        this.onControlClickListener = onControlClickListener;
    }

    public void setOnBrightClickListener02(OnBright02ClickListener OnBright02ClickListener) {
        this.OnBrightClickListener02 = OnBright02ClickListener;
    }

    public void setOnBright01ClickListener(OnBright01ClickListener OnBright02ClickListener) {
        this.OnBright01ClickListener = OnBright02ClickListener;
    }

    public void setOnBright03ClickListener(OnBright03ClickListener OnBright03ClickListener) {
        this.OnBright03ClickListener = OnBright03ClickListener;
    }

    public void setOnLlAdjustPlanListener(OnLlAdjustBrightnessPlanClickListener onLlAdjustBrightnessPlanClickListener) {
        this.onLlAdjustBrightnessPlanClickListener = onLlAdjustBrightnessPlanClickListener;
    }

    /**
     * 点击是否显示渐变
     */
    public interface OnControlClickListener {
        void onItemClick(View view, int position);
    }

    /**
     * 第一条时间亮度点击事件
     */
    public interface OnBright01ClickListener {
        void onItemClick(View view, int position);
    }

    /**
     * 第二条时间亮度点击事件
     */
    public interface OnBright02ClickListener {
        void onItemClick(View view, int position);
    }

    /**
     * 第三条时间亮度点击事件
     */
    public interface OnBright03ClickListener {
        void onItemClick(View view, int position);
    }

    public interface OnLlAdjustBrightnessPlanClickListener {
        void onClickListener(int position);
    }


    /**
     * 获取亮度数据
     *
     * @param a
     * @return
     */
    public List<ListParameterBean> getList(int a) {
        List<ListParameterBean> listParameterBeans = new ArrayList<>();
        List<LightDetailBean> lightDetailBeans = new ArrayList<>();
        for (int i = 0; i < mList.get(a).size(); i++) {
            lightDetailBeans.add(mList.get(0).get(i));
            listParameterBeans.add(mList.get(0).get(i).getProjectParameterList().get(0));
        }
        return listParameterBeans;
    }

    /**
     * 更新控制方案
     */
    public void upDateManager(int pos, String id, String name, String DimmingMode, List<ListParameterBean> parameterLists) {

        helper.AddProject(id, name, DimmingMode, parameterLists, new HttpHelper.HttpCallBack<String>() {
            @Override
            public void onSuccess(List<String> result) {
                Log.i("=====upDateManager成功", result.get(0));
                successToast();
//                EditDeviceGroupLight(mList.get(pos).get(0).getProjectId(), String.valueOf(pos + 1));
            }

            @SuppressLint("LongLogTag")
            @Override
            public void onFailure(final String message) {
                //  Log.v("=====upDateManager失败==onFailure", message);
                errorToast();
                // Toast.makeText(getActivity(), "更新方案失败" , Toast.LENGTH_LONG).show();
            }
        });
    }

    /**
     * 选择分组控制方案
     */
    public void EditDeviceGroupLight(String id, String num) {

        helper.EditDeviceGroupLight("32FFD7055643373914721851", num, id, new HttpHelper.HttpCallBack<String>() {
            @SuppressLint("LongLogTag")
            @Override
            public void onSuccess(List<String> result) {
                Log.i("=====EditDeviceGroupLight成功", result.get(0));
                successToast();
            }

            @SuppressLint("LongLogTag")
            @Override
            public void onFailure(final String message) {
                errorToast();
                //   Log.v("=====EditDeviceGroupLight==onFailure", message);
            }
        });
    }

    public void successToast() {
        Looper.prepare();
        Toast.makeText(context, "操作成功", Toast.LENGTH_SHORT).show();
        Looper.loop();
    }

    public void errorToast() {
        Looper.prepare();
        Toast.makeText(context, "操作失败", Toast.LENGTH_SHORT).show();
        Looper.loop();
    }
}
